package com.XiaoLongGroup.webtech.controller;/**
 * @author : SLCcccc
 * @date : 2023/4/11 14:15
 */

import com.XiaoLongGroup.webtech.VO.DeleteUserVO;
import com.XiaoLongGroup.webtech.entity.User;
import com.XiaoLongGroup.webtech.service.UserService;
import com.XiaoLongGroup.webtech.utils.Page;
import com.XiaoLongGroup.webtech.utils.PageManage;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *@ClassName UserController
 *@Description TODO
 *@Author SLCcccc
 *@Date 2023/4/11 14:15
 *@Version 1.0
 */

/**
 * article crud  user crud__表单重载问题
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserService userService;
    protected Map<String, Object> resultMap = new LinkedHashMap<>();

    @RequestMapping(value = "/loginDo", method = RequestMethod.POST)
    @ResponseBody
    public Object loginDo(@RequestBody User user, ModelMap modelMap, HttpSession session){
        try{
            user = userService.userLogin(user);
            if(user != null){
                resultMap.put("status", 200);
                resultMap.put("message", "登录成功");
                modelMap.addAttribute("loginUser", user);
                session.setAttribute("loginUser", user);
            }else{
                resultMap.put("status", 500);
                resultMap.put("message","账号或密码错误");
            }
        }catch(Exception e){
            resultMap.put("status", 406);
            resultMap.put("message", "wrong err");
        }
        return resultMap;
    }

    @RequestMapping(value = "/logonDo",method = RequestMethod.POST)
    @ResponseBody
    public Object logonDo(@RequestBody User user,ModelMap modelMap){
        System.out.println(user);
        try{
            if(userService.userLogon(user)){
                resultMap.put("status",200);
                resultMap.put("message","注册成功");
            }else{
                resultMap.put("status",500);
                resultMap.put("message","用户名或手机号已存在");
            }
        }catch(Exception e){
            resultMap.put("status",406);
            resultMap.put("message","系统出错");
            System.out.println(e.getMessage());
        }
        return resultMap;
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    @ResponseBody
    public Object userAdd(@RequestBody User user,ModelMap modelMap){
        try{
            if(userService.addUser(user)){
                resultMap.put("status",200);
                resultMap.put("message","注册成功");
            }else{
                resultMap.put("status",500);
                resultMap.put("message","用户名或手机号已存在");
            }
        }catch(Exception e){
            resultMap.put("status",406);
            resultMap.put("message","系统出错");
            System.out.println(e.getMessage());
        }
        return resultMap;
    }


    @RequestMapping(value = "/updateDo",method = RequestMethod.POST)
    @ResponseBody
    public Object updateDo(@RequestBody User user){
        try{
            if(userService.pwdReset(user)){
                resultMap.put("status",200);
                resultMap.put("message","修改成功");
            }else{
                resultMap.put("status",500);
                resultMap.put("message","用户名或手机号错误");
            }
        }catch(Exception e){
            resultMap.put("status",406);
            resultMap.put("message","系统出错");
            System.out.println(e.getMessage());
        }
        return resultMap;
    }

    /**
     * TODO 获取用户数据 渲染表格
     * @param page
     * @param limit
     * @param search
     * @return
     */
    @RequestMapping(value = "getUserList",method = RequestMethod.POST)
    @ResponseBody
    public Object getUserList(int page,int limit,String search){
        System.out.println(search);
        Page<User> userPage;
        if(StringUtils.isNoneBlank(search)){
            userPage = userService.getUserListByNameLike(search,page,limit);
        }else{
            userPage = userService.getUserList(page,limit);
            System.out.println(userPage);
        }
        return PageManage.buildPage(userPage);
    }

    /**
     * TODO 用户信息更新
     * @param user
     * @return
     */
    @RequestMapping(value ="/updateUser",method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestBody User user){
        try{
            if(userService.userInfoChange(user)){
                resultMap.put("status",200);
                resultMap.put("message","修改成功");
            }else{
                resultMap.put("status",500);
                resultMap.put("message","用户名或邮箱已存在");
            }
        }catch(Exception e){
            resultMap.put("status",406);
            resultMap.put("message","系统出错");
            System.out.println(e.getMessage());
        }
        return resultMap;
    }

    /**
     * TODO 展示用户编辑页面
     * @param userId
     * @return
     */
    @RequestMapping(value = "goUpdate",method = RequestMethod.GET)
    public ModelAndView goUpdate(@RequestParam("userId") String userId){
        ModelAndView modelAndView = new ModelAndView();
        User user = userService.getUserById(userId);
        modelAndView.addObject("user",user);
        modelAndView.setViewName("user/info");
        return modelAndView;
    }

    @RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteUser(@RequestBody DeleteUserVO userVO){
        System.out.println("userID:"+userVO.getUserId());
        try{
            System.out.println("1");
            if(userService.deleteUserByUserId(userVO.getUserId())){
                System.out.println("2");
                resultMap.put("status",200);
                resultMap.put("message","删除成功");
            }else{
                System.out.println("ss");
                resultMap.put("status",500);
                resultMap.put("message","系统出错");
            }
        }catch(Exception e){
            System.out.println("2122");
            resultMap.put("status",406);
            resultMap.put("message","系统出错");
        }
        System.out.println("23");
        return resultMap;
    }

    @RequestMapping(value = "/userBatchDel",method = RequestMethod.POST)
    @ResponseBody
    public Object userBatchDel(@RequestBody List<DeleteUserVO> userVOs){
        System.out.println(userVOs);
        boolean flag = true;
        List<String> errUserIdList = null;
        for (DeleteUserVO userVO : userVOs) {
            try{
                if(userService.deleteUserByUserId(userVO.getUserId())){
                }else{
                    errUserIdList.add(userVO.getUserId());
                    flag = false;
                }
            }catch(Exception e){
                resultMap.put("status",406);
                resultMap.put("message","系统出错");
            }
        }
        if(flag){
            resultMap.put("status",200);
            resultMap.put("message","删除成功");
        }else{
            resultMap.put("status",500);
            resultMap.put("message","系统出错,以下用户删除失败:" + errUserIdList);
        }
        return resultMap;
    }

    /**
     * 注销
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public @ResponseBody User
    logout(HttpServletRequest request){
        System.out.println("注销...");
        // 清空用户信息
        request.getSession().setAttribute("loginUser",null);
        return (User) request.getSession().getAttribute("loginUser");
    }

}
